import Vue from "vue";
import VueRouter from "vue-router";
import layout from "@/views/layout/index";
import home from "@/views/home/index.vue";
import login from "@/views/login/index.vue";
import store from "@/store";

const addTeacher = () => import("@/views/home/addTeacher.vue");
const timeRefer = () => import("@/views/timeRefer/index.vue");
const schTeacherTime = () => import("@/views/timeRefer/schTeacherTime/schTeacherTime.vue");
const schLabTime = () => import("@/views/timeRefer/schLabTime/schLabTime.vue");
const colTeacherTime = () => import("@/views/timeRefer/colTeacherTime/colTeacherTime.vue");
const colLabTime = () => import("@/views/timeRefer/colLabTime/colLabTime.vue");
const scoreNorm = () => import("@/views/scoreNorm/index.vue");
const teacherScore = () => import("@/views/scoreNorm/teacherScore/teacherScore.vue");
const labScore = () => import("@/views/scoreNorm/labScore/labScore.vue");
const managerScore = () => import("@/views/scoreNorm/managerScore/managerScore.vue");
const workScore = () => import("@/views/scoreNorm/workScore/workScore.vue");

Vue.use(VueRouter);
const originalPush = VueRouter.prototype.push;
// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

const routes = [
  {
    path: "/",
    component: layout,
    children: [
      {
        path: "/",
        name: "home",
        component: home,
      },
      {
        path: "/addTeacher",
        name: "addTeacher",
        component: addTeacher,
      },
      {
        path: "/timeRefer",
        name: "timeRefer",
        component: timeRefer,
        redirect: "/timeRefer/teacherTime",
        children: [
          {
            path: "schTeacherTime",
            name: "schTeacherTime",
            component: schTeacherTime,
          },
          {
            path: "schLabTime",
            name: "schLabTime",
            component: schLabTime,
          },
          {
            path: "colTeacherTime",
            name: "colTeacherTime",
            component: colTeacherTime,
          },
          {
            path: "colLabTime",
            name: "colLabTime",
            component: colLabTime,
          },
        ],
      },
      {
        path: "/scoreNorm",
        name: "scoreNorm",
        component: scoreNorm,
        redirect: "/scoreNorm/teacherScore",
        children: [
          {
            path: "teacherScore",
            name: "teacherScore",
            component: teacherScore,
          },
          {
            path: "labScore",
            name: "labScore",
            component: labScore,
          },
          {
            path: "managerScore",
            name: "managerScore",
            component: managerScore,
          },
          {
            path: "workScore",
            name: "workScore",
            component: workScore,
          },
        ],
      },
    ],
  },
  {
    path: "/login",
    name: "login",
    component: login,
  },
];

const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes,
});

const authUrls = ["/login"];

router.beforeEach((to, from, next) => {
  if (authUrls.includes(to.path)) {
    next();
    return;
  }
  const token = store.state.login.userinfo.token;
  if (token) {
    next();
  } else {
    next("/login");
  }
});

export default router;
